
<!DOCTYPE HTML>
<html lang="" >
    <head>
        <meta charset="UTF-8">
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <title>Java同步工具类- CountDownLatch&CyclicBarrier&Semaphore   · JAVA学习积累</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="description" content="">
        <meta name="generator" content="GitBook 3.2.3">
        <meta name="author" content="tianwyam">
        
        
    
    <link rel="stylesheet" href="../../gitbook/style.css">

    
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-tbfed-pagefooter/footer.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-splitter/splitter.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-pageview-count/plugin.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-anchor-navigation-ex/style/plugin.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-chapter-fold/chapter-fold.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-expandable-chapters-small/expandable-chapters-small.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-highlight/website.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-fontsettings/website.css">
                
            
        

    

    
        
    
        
    
        
    
        
    
        
    
        
    

        
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../../gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="../../gitbook/images/favicon.ico" type="image/x-icon">

    
    <link rel="next" href="../java_jvm.html" />
    
    
    <link rel="prev" href="java_multi_thread_tools.html" />
    

    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
            
                <nav role="navigation">
                


<ul class="summary">
    
    
    
        
        <li>
            <a href="https://blog.csdn.net/mybook201314" target="_blank" class="custom-link">博客Blog</a>
        </li>
    
    

    
    <li class="divider"></li>
    

    
        
        <li class="header">【前言】</li>
        
        
    
        <li class="chapter " data-level="1.1" data-path="../../">
            
                <a href="../../">
            
                    
                    介绍ABOUT
            
                </a>
            

            
        </li>
    

    
        
        <li class="header">【第一章】Java基础</li>
        
        
    
        <li class="chapter " data-level="2.1" data-path="../java.html">
            
                <a href="../java.html">
            
                    
                    计算机基础-操作系统-进程-计算机网络
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.2" data-path="../java_thread_pool.html">
            
                <a href="../java_thread_pool.html">
            
                    
                    线程池的工作原理
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.3" data-path="../java_ftp.html">
            
                <a href="../java_ftp.html">
            
                    
                    FTP文件服务器上传下载
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.4" data-path="../collection.html">
            
                <a href="../collection.html">
            
                    
                    集合
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="2.4.1" data-path="../collection/java_collection_map.html">
            
                <a href="../collection/java_collection_map.html">
            
                    
                    Map&HashMap&HashTable&ConcurrentHashMap
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.4.2" data-path="../java_queue.html">
            
                <a href="../java_queue.html">
            
                    
                    队列-queue
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.4.3" data-path="../java_tree.html">
            
                <a href="../java_tree.html">
            
                    
                    二叉树遍历
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    

    
        
        <li class="divider"></li>
        
        
    
        <li class="chapter " data-level="3.1" data-path="../java_io_nio.html">
            
                <a href="../java_io_nio.html">
            
                    
                    java输入输出流
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.2" data-path="../java_exception.html">
            
                <a href="../java_exception.html">
            
                    
                    异常exception
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.3" data-path="../java_serialize.html">
            
                <a href="../java_serialize.html">
            
                    
                    Java序列化
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.4" data-path="../Java_annotation.html">
            
                <a href="../Java_annotation.html">
            
                    
                    注解
            
                </a>
            

            
        </li>
    

    
        
        <li class="divider"></li>
        
        
    
        <li class="chapter " data-level="4.1" data-path="../multi.html">
            
                <a href="../multi.html">
            
                    
                    多线程
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="4.1.1" data-path="java_multi_thread_volatile_synchronized.html">
            
                <a href="java_multi_thread_volatile_synchronized.html">
            
                    
                    volatile&synchronized
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.1.2" data-path="java_multi_thread_object_wait_notify.html">
            
                <a href="java_multi_thread_object_wait_notify.html">
            
                    
                    object/wait/notify
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.1.3" data-path="java_multi_thread_thread.html">
            
                <a href="java_multi_thread_thread.html">
            
                    
                    thread
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.1.4" data-path="java_multi_thread_threadpool.html">
            
                <a href="java_multi_thread_threadpool.html">
            
                    
                    线程池的工作原理
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.1.5" data-path="java_multi_thread_lock.html">
            
                <a href="java_multi_thread_lock.html">
            
                    
                    lock&ReentrantLock&ReadWriteLock
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.1.6" data-path="java_multi_thread_lock_condition.html">
            
                <a href="java_multi_thread_lock_condition.html">
            
                    
                    lock&condition
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.1.7" data-path="java_multi_thread_tools.html">
            
                <a href="java_multi_thread_tools.html">
            
                    
                    Java并发工具- CountDownLatch&CyclicBarrier 
            
                </a>
            

            
        </li>
    
        <li class="chapter active" data-level="4.1.8" data-path="java_multi_thread_tools2.html">
            
                <a href="java_multi_thread_tools2.html">
            
                    
                    Java同步工具类- CountDownLatch&CyclicBarrier&Semaphore  
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    

    
        
        <li class="divider"></li>
        
        
    
        <li class="chapter " data-level="5.1" data-path="../java_jvm.html">
            
                <a href="../java_jvm.html">
            
                    
                    java虚拟机-JVM
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="5.2" data-path="../java_java7new.html">
            
                <a href="../java_java7new.html">
            
                    
                    java7新知识- Switch&TWR&Objects 
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="5.3" data-path="../java_stream.html">
            
                <a href="../java_stream.html">
            
                    
                    Java8对集合流操作stream
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="5.4" data-path="../java_stream_groupby.html">
            
                <a href="../java_stream_groupby.html">
            
                    
                    java8新语法-Stream-分组groupingBy操作 
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="5.5" data-path="../java_optional.html">
            
                <a href="../java_optional.html">
            
                    
                    java8新语法-Optional 
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="5.6" data-path="../java_8new_date_api.html">
            
                <a href="../java_8new_date_api.html">
            
                    
                    java8-新的日期时间API 
            
                </a>
            

            
        </li>
    

    
        
        <li class="divider"></li>
        
        
    
        <li class="chapter " data-level="6.1" data-path="../../xml/xml_translation.html">
            
                <a href="../../xml/xml_translation.html">
            
                    
                    XML中特殊符号的转译 
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="6.2" data-path="../../datastructure/binary_complement.html">
            
                <a href="../../datastructure/binary_complement.html">
            
                    
                    原码、反码、补码 
            
                </a>
            

            
        </li>
    

    
        
        <li class="divider"></li>
        
        
    
        <li class="chapter " data-level="7.1" data-path="../basic/javabean2map.html">
            
                <a href="../basic/javabean2map.html">
            
                    
                    Java bean转Map 
            
                </a>
            

            
        </li>
    

    
        
        <li class="header">【第二章】算法积累题</li>
        
        
    
        <li class="chapter " data-level="8.1" data-path="../../algorithm/merge_sorted_array.html">
            
                <a href="../../algorithm/merge_sorted_array.html">
            
                    
                    算法题 - 合并排序数组 
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="8.2" data-path="../../algorithm/lowest_score.html">
            
                <a href="../../algorithm/lowest_score.html">
            
                    
                    算法题 - 最低分数线问题 
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="8.3" data-path="../../algorithm/array_sub_list.html">
            
                <a href="../../algorithm/array_sub_list.html">
            
                    
                    算法题 - 求数组的子集合 
            
                </a>
            

            
        </li>
    

    
        
        <li class="header">【第三章】框架</li>
        
        
    
        <li class="chapter " data-level="9.1" data-path="../../Spring/springboot.html">
            
                <a href="../../Spring/springboot.html">
            
                    
                    SpringBoot
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="9.2" data-path="../../Spring/spring_security.html">
            
                <a href="../../Spring/spring_security.html">
            
                    
                    SpringSecurity
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="9.3" data-path="../../linux/linux_commands_springboot_startup.html">
            
                <a href="../../linux/linux_commands_springboot_startup.html">
            
                    
                    SpringBoot项目启动脚本
            
                </a>
            

            
        </li>
    

    
        
        <li class="divider"></li>
        
        
    
        <li class="chapter " data-level="10.1" data-path="../../Spring/spring/spring_interview_questions_68.html">
            
                <a href="../../Spring/spring/spring_interview_questions_68.html">
            
                    
                    Spring面试题68
            
                </a>
            

            
        </li>
    

    
        
        <li class="divider"></li>
        
        
    
        <li class="chapter " data-level="11.1" data-path="../../Spring/springboot_auto_config.html">
            
                <a href="../../Spring/springboot_auto_config.html">
            
                    
                    SpringBoot自动装载机制 - 源码分析
            
                </a>
            

            
        </li>
    

    
        
        <li class="divider"></li>
        
        
    
        <li class="chapter " data-level="12.1" data-path="../../Spring/spring_feign.html">
            
                <a href="../../Spring/spring_feign.html">
            
                    
                    Feign在spring、springboot下使用
            
                </a>
            

            
        </li>
    

    
        
        <li class="divider"></li>
        
        
    
        <li class="chapter " data-level="13.1" data-path="../../Spring/spring_freemarker.html">
            
                <a href="../../Spring/spring_freemarker.html">
            
                    
                    Spring boot集成freemarker，生成word文件
            
                </a>
            

            
        </li>
    

    
        
        <li class="divider"></li>
        
        
    
        <li class="chapter " data-level="14.1" data-path="../../Spring/spring_freemarker_generate.html">
            
                <a href="../../Spring/spring_freemarker_generate.html">
            
                    
                    Freemarker自动生成Java bean代码
            
                </a>
            

            
        </li>
    

    
        
        <li class="divider"></li>
        
        
    
        <li class="chapter " data-level="15.1" data-path="../../mybatis/mybatis_metadata.html">
            
                <a href="../../mybatis/mybatis_metadata.html">
            
                    
                    mybatis中操作元数据
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="15.2" data-path="../../mybatis/mybatis_batch_exec_sql.html">
            
                <a href="../../mybatis/mybatis_batch_exec_sql.html">
            
                    
                    mybatis中insert/update/delete标签中执行多条SQL语句
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="15.3" data-path="../../mybatis/mybatis_plus.html">
            
                <a href="../../mybatis/mybatis_plus.html">
            
                    
                    springboot集成mybatis-plus框架
            
                </a>
            

            
        </li>
    

    
        
        <li class="divider"></li>
        
        
    
        <li class="chapter " data-level="16.1" data-path="../../distributed/sharding-jdbc.html">
            
                <a href="../../distributed/sharding-jdbc.html">
            
                    
                    【分布式】sharding-jdbc实现分库分表
            
                </a>
            

            
        </li>
    

    
        
        <li class="header">【第四章】数据库</li>
        
        
    
        <li class="chapter " data-level="17.1" data-path="../../SQL/sql.html">
            
                <a href="../../SQL/sql.html">
            
                    
                    SQL常用使用
            
                </a>
            

            
        </li>
    

    
        
        <li class="divider"></li>
        
        
    
        <li class="chapter " data-level="18.1" data-path="../../redis/redis_study_notes.html">
            
                <a href="../../redis/redis_study_notes.html">
            
                    
                    redis学习笔记
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="18.2" data-path="../../redis/redis_list_second_kill.html">
            
                <a href="../../redis/redis_list_second_kill.html">
            
                    
                    Redis中的队列list实现秒杀活动抢购
            
                </a>
            

            
        </li>
    

    
        
        <li class="divider"></li>
        
        
    
        <li class="chapter " data-level="19.1" data-path="../../SQL/sql_mysql_green_install.html">
            
                <a href="../../SQL/sql_mysql_green_install.html">
            
                    
                    mysql绿色版安装服务教程
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="19.2" data-path="../../SQL/sql_h2.html">
            
                <a href="../../SQL/sql_h2.html">
            
                    
                    H2内嵌数据的使用
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="19.3" data-path="../../SQL/cache.html">
            
                <a href="../../SQL/cache.html">
            
                    
                    缓存
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="19.4" data-path="../../SQL/mysql/mysql_back_rollback.html">
            
                <a href="../../SQL/mysql/mysql_back_rollback.html">
            
                    
                    MySQL单表备份还原
            
                </a>
            

            
        </li>
    

    
        
        <li class="header">【第五章】Web</li>
        
        
    
        <li class="chapter " data-level="20.1" data-path="../../web/web_js_jquery.html">
            
                <a href="../../web/web_js_jquery.html">
            
                    
                    web简单积累
            
                </a>
            

            
        </li>
    

    
        
        <li class="header">【第六章】Linux</li>
        
        
    
        <li class="chapter " data-level="21.1" data-path="../../linux/linux_common_commands.html">
            
                <a href="../../linux/linux_common_commands.html">
            
                    
                    linux常用命令
            
                </a>
            

            
        </li>
    

    
        
        <li class="header">【第七章】Windows</li>
        
        
    
        <li class="chapter " data-level="22.1" data-path="../../windows/windows_common_commands.html">
            
                <a href="../../windows/windows_common_commands.html">
            
                    
                    windows常用命令
            
                </a>
            

            
        </li>
    

    
        
        <li class="header">【第八章】错误集</li>
        
        
    
        <li class="chapter " data-level="23.1" data-path="../../error/maven_not_found_local_jar.html">
            
                <a href="../../error/maven_not_found_local_jar.html">
            
                    
                    maven引入依赖时识别不到本地仓库的jar
            
                </a>
            

            
        </li>
    

    
        
        <li class="divider"></li>
        
        
    
        <li class="chapter " data-level="24.1" data-path="../../error/security_not_configure_antMatchers_after_anyRequest.html">
            
                <a href="../../error/security_not_configure_antMatchers_after_anyRequest.html">
            
                    
                    spring security 配置发生错误 Can't configure antMatchers after anyRequest
            
                </a>
            

            
        </li>
    

    
        
        <li class="divider"></li>
        
        
    
        <li class="chapter " data-level="25.1" data-path="../../error/AOP_stream_close_error.html">
            
                <a href="../../error/AOP_stream_close_error.html">
            
                    
                    AOP内获取了对请求流操作后报流已经关闭错误的问题
            
                </a>
            

            
        </li>
    

    
        
        <li class="divider"></li>
        
        
    
        <li class="chapter " data-level="26.1" data-path="../../tools/github/github_ssh.html">
            
                <a href="../../tools/github/github_ssh.html">
            
                    
                    Github ssh key 解决提交后push频繁输入用户名和密码
            
                </a>
            

            
        </li>
    

    
        
        <li class="divider"></li>
        
        
    
        <li class="chapter " data-level="27.1" data-path="../../error/sharding-jdbc-error-datasources.html">
            
                <a href="../../error/sharding-jdbc-error-datasources.html">
            
                    
                    sharding-jdbc-4.1.1版本启动数据源报错问题
            
                </a>
            

            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
            Published with GitBook
        </a>
    </li>
</ul>


                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="../.." >Java同步工具类- CountDownLatch&CyclicBarrier&Semaphore  </a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
                                <section class="normal markdown-section">
                                
                                <div id="anchor-navigation-ex-navbar"><i class="fa fa-navicon"></i><ul><li><span class="title-icon "></span><a href="#java&#x540C;&#x6B65;&#x5DE5;&#x5177;&#x7C7B;"><b></b>Java&#x540C;&#x6B65;&#x5DE5;&#x5177;&#x7C7B;</a></li><ul><li><span class="title-icon "></span><a href="#1-countdownlatch-&#x8BA1;&#x6570;&#x5668;"><b></b>1. CountDownLatch &#x8BA1;&#x6570;&#x5668;</a></li><ul><li><span class="title-icon "></span><a href="#11-&#x793A;&#x4F8B;"><b></b>1.1 &#x793A;&#x4F8B;</a></li><li><span class="title-icon "></span><a href="#12-&#x8FD0;&#x884C;&#x6548;&#x679C;"><b></b>1.2 &#x8FD0;&#x884C;&#x6548;&#x679C;</a></li></ul><li><span class="title-icon "></span><a href="#2-cyclicbarrier-&#x5C4F;&#x969C;"><b></b>2. CyclicBarrier &#x5C4F;&#x969C;</a></li><ul><li><span class="title-icon "></span><a href="#21-&#x793A;&#x4F8B;"><b></b>2.1 &#x793A;&#x4F8B;</a></li><li><span class="title-icon "></span><a href="#22-&#x8FD0;&#x884C;&#x7ED3;&#x679C;"><b></b>2.2 &#x8FD0;&#x884C;&#x7ED3;&#x679C;</a></li></ul><li><span class="title-icon "></span><a href="#3-semaphore-&#x4FE1;&#x53F7;&#x91CF;"><b></b>3. Semaphore &#x4FE1;&#x53F7;&#x91CF;</a></li><ul><li><span class="title-icon "></span><a href="#31-&#x793A;&#x4F8B;"><b></b>3.1 &#x793A;&#x4F8B;</a></li><li><span class="title-icon "></span><a href="#32-&#x8FD0;&#x884C;&#x7ED3;&#x679C;"><b></b>3.2 &#x8FD0;&#x884C;&#x7ED3;&#x679C;</a></li></ul></ul></ul></div><h1 id="java&#x540C;&#x6B65;&#x5DE5;&#x5177;&#x7C7B;"><a name="java&#x540C;&#x6B65;&#x5DE5;&#x5177;&#x7C7B;" class="anchor-navigation-ex-anchor" href="#java&#x540C;&#x6B65;&#x5DE5;&#x5177;&#x7C7B;"><i class="fa fa-link" aria-hidden="true"></i></a>Java&#x540C;&#x6B65;&#x5DE5;&#x5177;&#x7C7B;</h1>
<p><br></p>
<ul>
<li>CountDownLatch &#x8BA1;&#x6570;&#x5668;</li>
<li>CyclicBarrier &#x5C4F;&#x969C;</li>
<li>Semaphore &#x4FE1;&#x53F7;&#x91CF;</li>
</ul>
<p><br><br></p>
<h2 id="1-countdownlatch-&#x8BA1;&#x6570;&#x5668;"><a name="1-countdownlatch-&#x8BA1;&#x6570;&#x5668;" class="anchor-navigation-ex-anchor" href="#1-countdownlatch-&#x8BA1;&#x6570;&#x5668;"><i class="fa fa-link" aria-hidden="true"></i></a>1. CountDownLatch &#x8BA1;&#x6570;&#x5668;</h2>
<p><br></p>
<p>CountDownLatch &#xFF1A;&#x5141;&#x8BB8;&#x4E00;&#x4E2A;&#x6216;&#x591A;&#x4E2A;&#x7EBF;&#x7A0B;&#x7B49;&#x5F85;&#x5176;&#x4ED6;&#x7EBF;&#x7A0B;&#x5B8C;&#x6210;&#x64CD;&#x4F5C;</p>
<p><br></p>
<pre><code class="lang-java"><span class="hljs-comment">/*
 * &#x540C;&#x6B65;&#x5DE5;&#x5177;&#x7C7B;&#xFF1A;CountDownLatch &#x8BA1;&#x6570;&#x5668;
 * &#x5185;&#x90E8;&#x6709;&#x4E2A;&#x8BA1;&#x6570;&#x5668;&#xFF0C;&#x53EA;&#x6709;&#x7B49;&#x8BA1;&#x6570;&#x5668;&#x4E3A;0&#x65F6;&#xFF0C; latch.await() &#x624D;&#x80FD;&#x7EE7;&#x7EED;&#x5F80;&#x4E0B;&#x8D70;
 * latch.countDown() &#x5C31;&#x662F;&#x6BCF;&#x4E00;&#x6B21; &#x8BA1;&#x6570; &#x51CF;&#x4E00; 
 * &#x5B50;&#x7EBF;&#x7A0B;&#x4E0D;&#x9700;&#x8981;&#x7B49;&#x5F85;&#xFF0C;&#x4F1A;&#x7EE7;&#x7EED;&#x5F80;&#x4E0B;&#x6267;&#x884C;
 */</span>
</code></pre>
<p><br><br></p>
<h3 id="11-&#x793A;&#x4F8B;"><a name="11-&#x793A;&#x4F8B;" class="anchor-navigation-ex-anchor" href="#11-&#x793A;&#x4F8B;"><i class="fa fa-link" aria-hidden="true"></i></a>1.1 &#x793A;&#x4F8B;</h3>
<p><br></p>
<p>&#x793A;&#x4F8B;&#xFF1A;&#x5B66;&#x751F;&#x7B7E;&#x5230;</p>
<p>&#x4E3B;&#x7EBF;&#x7A0B;&#xFF1A;&#x6A21;&#x62DF;&#x8001;&#x5E08;&#x7B49;&#x5F85;&#x5B66;&#x751F;&#x7B7E;&#x5230;&#xFF0C;&#x53EA;&#x6709;&#x7B49;&#x6240;&#x6709;&#x7684;&#x5B66;&#x751F;&#x7B7E;&#x5B8C;&#x5230;&#xFF0C;&#x8001;&#x5E08;&#x624D;&#x80FD;&#x8D70;</p>
<p>&#x5B50;&#x7EBF;&#x7A0B;&#xFF1A;&#x6A21;&#x62DF;&#x5B66;&#x751F;&#x7B7E;&#x5230;&#xFF0C;&#x7B7E;&#x5B8C;&#x5230;&#xFF0C;&#x53EF;&#x4EE5;&#x8D70;&#x4E86;</p>
<p><br></p>
<p>&#x65B0;&#x5EFA;&#x4E00;&#x4E2A;&#x7EBF;&#x7A0B;&#x7C7B;&#xFF0C;&#x6A21;&#x62DF;&#x5B66;&#x751F;&#x7B7E;&#x5230;</p>
<pre><code class="lang-java">
<span class="hljs-comment">/**
 * <span class="hljs-doctag">@description</span>
 *    &#x6A21;&#x62DF; &#x5B66;&#x751F; &#x7B7E;&#x5230; &#x7EBF;&#x7A0B;
 * <span class="hljs-doctag">@author</span> TianwYam
 * <span class="hljs-doctag">@date</span> 2021&#x5E74;6&#x6708;30&#x65E5;&#x4E0B;&#x5348;5:30:59
 */</span>
<span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">SignInThread</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">Thread</span> </span>{

    <span class="hljs-keyword">private</span> String name;
    <span class="hljs-keyword">private</span> CountDownLatch latch ;

    <span class="hljs-function"><span class="hljs-keyword">private</span> <span class="hljs-title">SignInThread</span><span class="hljs-params">(String name, CountDownLatch latch)</span> </span>{
        <span class="hljs-keyword">super</span>(name);
        <span class="hljs-keyword">this</span>.name = name ;
        <span class="hljs-keyword">this</span>.latch = latch ;
    }

    <span class="hljs-meta">@Override</span>
    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">run</span><span class="hljs-params">()</span> </span>{

        System.out.println(name + <span class="hljs-string">&quot;: &#x7B7E;&#x5B8C;&#x5230;&#x4E86;&quot;</span>);
        latch.countDown();
        System.out.println(name + <span class="hljs-string">&quot;: &#x8D70;&#x4E86;&quot;</span>);

    }
}
</code></pre>
<p><br><br></p>
<p>&#x4E3B;&#x7EBF;&#x7A0B;&#xFF1A;</p>
<pre><code class="lang-java">
<span class="hljs-comment">/*
 * &#x540C;&#x6B65;&#x5DE5;&#x5177;&#x7C7B;&#xFF1A;CountDownLatch &#x8BA1;&#x6570;&#x5668;
 * &#x5185;&#x90E8;&#x6709;&#x4E2A;&#x8BA1;&#x6570;&#x5668;&#xFF0C;&#x53EA;&#x6709;&#x7B49;&#x8BA1;&#x6570;&#x5668;&#x4E3A;0&#x65F6;&#xFF0C; latch.await() &#x624D;&#x80FD;&#x7EE7;&#x7EED;&#x5F80;&#x4E0B;&#x8D70;
 * latch.countDown() &#x5C31;&#x662F;&#x6BCF;&#x4E00;&#x6B21; &#x8BA1;&#x6570; &#x51CF;&#x4E00; 
 * &#x5B50;&#x7EBF;&#x7A0B;&#x4E0D;&#x9700;&#x8981;&#x7B49;&#x5F85;&#xFF0C;&#x4F1A;&#x7EE7;&#x7EED;&#x5F80;&#x4E0B;&#x6267;&#x884C;
 */</span>


<span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title">main</span><span class="hljs-params">(String[] args)</span> </span>{

    <span class="hljs-comment">// &#x8BA1;&#x6570;&#x5668;</span>
    CountDownLatch latch = <span class="hljs-keyword">new</span> CountDownLatch(<span class="hljs-number">2</span>);

    <span class="hljs-keyword">new</span> SignInThread(<span class="hljs-string">&quot;&#x5B66;&#x751F;1&quot;</span>, latch).start();
    <span class="hljs-keyword">new</span> SignInThread(<span class="hljs-string">&quot;&#x5B66;&#x751F;2&quot;</span>, latch).start();


    <span class="hljs-keyword">try</span> {
        System.out.println(<span class="hljs-string">&quot;&#x8001;&#x5E08;&#x7B49;&#x5B66;&#x751F;&#x7B7E;&#x5230;&quot;</span>);
        latch.await();
        System.out.println(<span class="hljs-string">&quot;&#x6240;&#x6709;&#x7684;&#x5B66;&#x751F;&#x90FD;&#x7B7E;&#x5B8C;&#x5230;&#x4E86;&#xFF0C;&#x8001;&#x5E08;&#x4E5F;&#x8D70;&#x4E86;&quot;</span>);
    } <span class="hljs-keyword">catch</span> (InterruptedException e) {
        e.printStackTrace();
    }

}
</code></pre>
<p><br><br></p>
<h3 id="12-&#x8FD0;&#x884C;&#x6548;&#x679C;"><a name="12-&#x8FD0;&#x884C;&#x6548;&#x679C;" class="anchor-navigation-ex-anchor" href="#12-&#x8FD0;&#x884C;&#x6548;&#x679C;"><i class="fa fa-link" aria-hidden="true"></i></a>1.2 &#x8FD0;&#x884C;&#x6548;&#x679C;</h3>
<p><br></p>
<pre><code class="lang-java">&#x8001;&#x5E08;&#x7B49;&#x5B66;&#x751F;&#x7B7E;&#x5230;
&#x5B66;&#x751F;<span class="hljs-number">1</span>: &#x7B7E;&#x5B8C;&#x5230;&#x4E86;
&#x5B66;&#x751F;<span class="hljs-number">1</span>: &#x8D70;&#x4E86;
&#x5B66;&#x751F;<span class="hljs-number">2</span>: &#x7B7E;&#x5B8C;&#x5230;&#x4E86;
&#x5B66;&#x751F;<span class="hljs-number">2</span>: &#x8D70;&#x4E86;
&#x6240;&#x6709;&#x7684;&#x5B66;&#x751F;&#x90FD;&#x7B7E;&#x5B8C;&#x5230;&#x4E86;&#xFF0C;&#x8001;&#x5E08;&#x4E5F;&#x8D70;&#x4E86;
</code></pre>
<p><br><br></p>
<h2 id="2-cyclicbarrier-&#x5C4F;&#x969C;"><a name="2-cyclicbarrier-&#x5C4F;&#x969C;" class="anchor-navigation-ex-anchor" href="#2-cyclicbarrier-&#x5C4F;&#x969C;"><i class="fa fa-link" aria-hidden="true"></i></a>2. CyclicBarrier &#x5C4F;&#x969C;</h2>
<p><br></p>
<p>CyclicBarrier&#xFF1A;&#x540C;&#x6B65;&#x5C4F;&#x969C;&#xFF0C;&#x5B83;&#x8981;&#x505A;&#x662F;&#x4E8B;&#x60C5;&#x5C31;&#x662F;&#x8BA9;&#x4E00;&#x7EC4;&#x7EBF;&#x7A0B;&#x5230;&#x8FBE;&#x4E00;&#x4E2A;&#x5C4F;&#x969C;(&#x4E5F;&#x53EF;&#x4EE5;&#x53EB;&#x540C;&#x6B65;)&#x65F6;&#x88AB;&#x963B;&#x585E;&#xFF0C;&#x76F4;&#x5230;&#x6700;&#x540E;&#x4E00;&#x4E2A;&#x7EBF;&#x7A0B;&#x5230;&#x8FBE;&#x5C4F;&#x969C;&#x65F6;&#xFF0C;&#x5C4F;&#x969C;&#x624D;&#x4F1A;&#x5F00;&#x95E8;&#xFF0C;&#x6240;&#x6709;&#x88AB;&#x5C4F;&#x969C;&#x62E6;&#x622A;&#x7684;&#x7EBF;&#x7A0B;&#x624D;&#x4F1A;&#x7EE7;&#x7EED;&#x8FD0;&#x884C;&#x3002;</p>
<p><br></p>
<pre><code class="lang-java"><span class="hljs-comment">/*
 * &#x540C;&#x6B65;&#x5DE5;&#x5177;&#x7C7B;&#xFF1A;CyclicBarrier &#x5C4F;&#x969C;
 * &#x5982;&#x540C;&#x6709;&#x4E00;&#x4E2A;&#x5C4F;&#x969C; &#x628A;&#x6240;&#x6709;&#x7EBF;&#x7A0B;&#x90FD; &#x9694;&#x5F00;&#x5230;&#x4E00;&#x8FB9;&#xFF0C;&#x53EA;&#x6709;&#x6240;&#x6709;&#x7684;&#x7EBF;&#x7A0B; &#x7B49;&#x5F85;&#x5C31;&#x7EEA;&#x540E;&#xFF0C;&#x5C4F;&#x969C;&#x624D;&#x6253;&#x5F00;
 * barrier.await() &#x5C31;&#x662F;&#x7B49;&#x5F85;&#x5C31;&#x7EEA;&#xFF0C;&#x9700;&#x8981;&#x7B49;&#x6240;&#x6709;&#x7684;&#x6267;&#x884C;&#x4E86;  await &#x540E;&#xFF0C;&#x624D;&#x80FD;&#x7EE7;&#x7EED;&#x5F80;&#x4E0B;&#x8D70;
 * &#x5B50;&#x7EBF;&#x7A0B; &#x9700;&#x8981;&#x4E92;&#x76F8;&#x7B49;&#x5F85;&#xFF0C;&#x624D;&#x4F1A;&#x7EE7;&#x7EED;&#x5F80;&#x4E0B;&#x6267;&#x884C;
 */</span>
</code></pre>
<p><br><br></p>
<h3 id="21-&#x793A;&#x4F8B;"><a name="21-&#x793A;&#x4F8B;" class="anchor-navigation-ex-anchor" href="#21-&#x793A;&#x4F8B;"><i class="fa fa-link" aria-hidden="true"></i></a>2.1 &#x793A;&#x4F8B;</h3>
<p><br></p>
<p>&#x793A;&#x4F8B;&#xFF1A;&#x6A21;&#x62DF;&#x73A9;&#x6E38;&#x620F;</p>
<p>&#x5B50;&#x7EBF;&#x7A0B;&#xFF1A;&#x6240;&#x6709;&#x73A9;&#x5BB6;&#x51C6;&#x5907;&#x597D;&#x4E86;&#xFF0C;&#x624D;&#x80FD;&#x8FDB;&#x5165;&#x6E38;&#x620F;</p>
<p><br></p>
<p>&#x65B0;&#x5EFA;&#x4E00;&#x4E2A;&#x7EBF;&#x7A0B;&#x7C7B;&#xFF0C;&#x6A21;&#x62DF;&#x73A9;&#x5BB6;&#x73A9;&#x6E38;&#x620F;</p>
<pre><code class="lang-java">
<span class="hljs-comment">/**
 * <span class="hljs-doctag">@description</span>
 *    &#x6A21;&#x62DF;&#x73A9;&#x6E38;&#x620F;&#x7684; &#x7EBF;&#x7A0B;
 * <span class="hljs-doctag">@author</span> TianwYam
 * <span class="hljs-doctag">@date</span> 2021&#x5E74;6&#x6708;30&#x65E5;&#x4E0B;&#x5348;5:30:38
 */</span>
<span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">PlayGameThread</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">Thread</span> </span>{

    <span class="hljs-keyword">private</span> String name;
    <span class="hljs-keyword">private</span> CyclicBarrier barrier ;

    <span class="hljs-function"><span class="hljs-keyword">private</span> <span class="hljs-title">PlayGameThread</span><span class="hljs-params">(String name, CyclicBarrier barrier)</span> </span>{
        <span class="hljs-keyword">super</span>(name);
        <span class="hljs-keyword">this</span>.name = name ;
        <span class="hljs-keyword">this</span>.barrier = barrier ;
    }

    <span class="hljs-meta">@Override</span>
    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">run</span><span class="hljs-params">()</span> </span>{

        System.out.println(name + <span class="hljs-string">&quot;: &#x51C6;&#x5907;&#x5B8C;&#x6BD5;&quot;</span>);
        <span class="hljs-keyword">try</span> {
            barrier.await();
        } <span class="hljs-keyword">catch</span> (InterruptedException | BrokenBarrierException e) {
            e.printStackTrace();
        }
        System.out.println(name + <span class="hljs-string">&quot;: &#x8FDB;&#x5165;&#x6E38;&#x620F;&quot;</span>);

    }
}
</code></pre>
<p><br></p>
<p>&#x4E3B;&#x7EBF;&#x7A0B;&#xFF1A;</p>
<pre><code class="lang-java">
<span class="hljs-comment">/*
 * &#x540C;&#x6B65;&#x5DE5;&#x5177;&#x7C7B;&#xFF1A;CyclicBarrier &#x5C4F;&#x969C;
 * &#x5982;&#x540C;&#x6709;&#x4E00;&#x4E2A;&#x5C4F;&#x969C; &#x628A;&#x6240;&#x6709;&#x7EBF;&#x7A0B;&#x90FD; &#x9694;&#x5F00;&#x5230;&#x4E00;&#x8FB9;&#xFF0C;&#x53EA;&#x6709;&#x6240;&#x6709;&#x7684;&#x7EBF;&#x7A0B; &#x7B49;&#x5F85;&#x5C31;&#x7EEA;&#x540E;&#xFF0C;&#x5C4F;&#x969C;&#x624D;&#x6253;&#x5F00;
 * barrier.await() &#x5C31;&#x662F;&#x7B49;&#x5F85;&#x5C31;&#x7EEA;&#xFF0C;&#x9700;&#x8981;&#x7B49;&#x6240;&#x6709;&#x7684;&#x6267;&#x884C;&#x4E86;  await &#x540E;&#xFF0C;&#x624D;&#x80FD;&#x7EE7;&#x7EED;&#x5F80;&#x4E0B;&#x8D70;
 * &#x5B50;&#x7EBF;&#x7A0B; &#x9700;&#x8981;&#x4E92;&#x76F8;&#x7B49;&#x5F85;&#xFF0C;&#x624D;&#x4F1A;&#x7EE7;&#x7EED;&#x5F80;&#x4E0B;&#x6267;&#x884C;
 */</span>

<span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title">main</span><span class="hljs-params">(String[] args)</span> </span>{

    <span class="hljs-comment">// &#x5C4F;&#x969C; &#x6A21;&#x62DF; 3&#x4E2A;&#x73A9;&#x5BB6;&#x73A9;&#x6E38;&#x620F;</span>
    <span class="hljs-comment">// CyclicBarrier barrier = new CyclicBarrier(3);</span>

    <span class="hljs-comment">// CyclicBarrier(int parties, Runnable barrierAction)</span>
    <span class="hljs-comment">// &#x5F53;&#x6240;&#x6709;&#x7684;&#x7EBF;&#x7A0B;&#x90FD;&#x8FBE;&#x5230;&#x5C4F;&#x969C;&#x540E;&#xFF0C;&#x4F18;&#x5148;&#x6267;&#x884C; barrierAction</span>
    CyclicBarrier barrier = <span class="hljs-keyword">new</span> CyclicBarrier(<span class="hljs-number">3</span>, ()-&gt;{
        System.out.println(<span class="hljs-string">&quot;&#x6240;&#x6709;&#x73A9;&#x5BB6;&#x5DF2;&#x51C6;&#x5907;&#x597D;&#xFF0C;&#x5F00;&#x59CB;&#x73A9;&#x6E38;&#x620F;&quot;</span>);
    });

    System.out.println(<span class="hljs-string">&quot;&#x7B49;&#x6240;&#x6709;&#x73A9;&#x5BB6;&#x90FD;&#x51C6;&#x5907;&#x597D;&#x4E86;&#xFF0C;&#x624D;&#x80FD;&#x5F00;&#x59CB;&#x6E38;&#x620F;&#xFF1A;&quot;</span>);

    <span class="hljs-keyword">new</span> PlayGameThread(<span class="hljs-string">&quot;&#x73A9;&#x5BB6;1&quot;</span>, barrier).start();
    <span class="hljs-keyword">new</span> PlayGameThread(<span class="hljs-string">&quot;&#x73A9;&#x5BB6;2&quot;</span>, barrier).start();
    <span class="hljs-keyword">new</span> PlayGameThread(<span class="hljs-string">&quot;&#x73A9;&#x5BB6;3&quot;</span>, barrier).start();

}
</code></pre>
<p><br><br></p>
<h3 id="22-&#x8FD0;&#x884C;&#x7ED3;&#x679C;"><a name="22-&#x8FD0;&#x884C;&#x7ED3;&#x679C;" class="anchor-navigation-ex-anchor" href="#22-&#x8FD0;&#x884C;&#x7ED3;&#x679C;"><i class="fa fa-link" aria-hidden="true"></i></a>2.2 &#x8FD0;&#x884C;&#x7ED3;&#x679C;</h3>
<p><br></p>
<pre><code class="lang-java">&#x7B49;&#x6240;&#x6709;&#x73A9;&#x5BB6;&#x90FD;&#x51C6;&#x5907;&#x597D;&#x4E86;&#xFF0C;&#x624D;&#x80FD;&#x5F00;&#x59CB;&#x6E38;&#x620F;&#xFF1A;
&#x73A9;&#x5BB6;<span class="hljs-number">1</span>: &#x51C6;&#x5907;&#x5B8C;&#x6BD5;
&#x73A9;&#x5BB6;<span class="hljs-number">2</span>: &#x51C6;&#x5907;&#x5B8C;&#x6BD5;
&#x73A9;&#x5BB6;<span class="hljs-number">3</span>: &#x51C6;&#x5907;&#x5B8C;&#x6BD5;
&#x6240;&#x6709;&#x73A9;&#x5BB6;&#x5DF2;&#x51C6;&#x5907;&#x597D;&#xFF0C;&#x5F00;&#x59CB;&#x73A9;&#x6E38;&#x620F;
&#x73A9;&#x5BB6;<span class="hljs-number">3</span>: &#x8FDB;&#x5165;&#x6E38;&#x620F;
&#x73A9;&#x5BB6;<span class="hljs-number">1</span>: &#x8FDB;&#x5165;&#x6E38;&#x620F;
&#x73A9;&#x5BB6;<span class="hljs-number">2</span>: &#x8FDB;&#x5165;&#x6E38;&#x620F;
</code></pre>
<p><br><br></p>
<h2 id="3-semaphore-&#x4FE1;&#x53F7;&#x91CF;"><a name="3-semaphore-&#x4FE1;&#x53F7;&#x91CF;" class="anchor-navigation-ex-anchor" href="#3-semaphore-&#x4FE1;&#x53F7;&#x91CF;"><i class="fa fa-link" aria-hidden="true"></i></a>3. Semaphore &#x4FE1;&#x53F7;&#x91CF;</h2>
<p><br></p>
<p>Semaphore &#x4FE1;&#x53F7;&#x91CF;&#xFF0C;&#x53EA;&#x6709;&#x62FF;&#x5230;&#x4E86;&#x4FE1;&#x53F7;&#x91CF;&#xFF0C;&#x624D;&#x80FD;&#x6267;&#x884C;&#xFF0C;&#x6267;&#x884C;&#x5B8C;&#x540E;&#xFF0C;&#x5F52;&#x8FD8;&#x4FE1;&#x53F7;&#x91CF;&#xFF0C;&#x6CA1;&#x6709;&#x62FF;&#x5230;&#x9700;&#x8981;&#x7B49;&#x5F85;</p>
<p><br></p>
<pre><code class="lang-java"><span class="hljs-comment">/*
 * &#x540C;&#x6B65;&#x5DE5;&#x5177;&#x7C7B;&#xFF1A;Semaphore &#x4FE1;&#x53F7;&#x91CF;
 * &#x5B9A;&#x4E49;&#x603B;&#x7684;&#x4FE1;&#x53F7;&#x91CF;&#xFF0C;&#x62FF;&#x5230;&#x4E86;&#xFF0C;&#x624D;&#x80FD;&#x6267;&#x884C;&#xFF0C;&#x6267;&#x884C;&#x5B8C;&#x540E;&#xFF0C;&#x5F52;&#x8FD8;&#x4FE1;&#x53F7;&#x91CF;&#xFF0C;&#x6CA1;&#x6709;&#x62FF;&#x5230;&#x9700;&#x8981;&#x7B49;&#x5F85;
 * semaphore.tryAcquire() &#x5C1D;&#x8BD5;&#x83B7;&#x53D6; &#x4FE1;&#x53F7;&#x91CF;
 * semaphore.release() &#x91CA;&#x653E;&#x4FE1;&#x53F7;&#x91CF;
 * &#x53EF;&#x7528;&#x4E8E;&#x6D41;&#x91CF;&#x63A7;&#x5236;&#x3001;&#x524A;&#x5CF0;
 */</span>
</code></pre>
<p><br><br></p>
<h3 id="31-&#x793A;&#x4F8B;"><a name="31-&#x793A;&#x4F8B;" class="anchor-navigation-ex-anchor" href="#31-&#x793A;&#x4F8B;"><i class="fa fa-link" aria-hidden="true"></i></a>3.1 &#x793A;&#x4F8B;</h3>
<p><br></p>
<p>&#x793A;&#x4F8B;&#xFF1A;&#x5B66;&#x751F;&#x62A2;&#x996D;&#x5361;&#xFF0C;&#x62A2;&#x5230;&#x4E86;&#x624D;&#x80FD;&#x53BB;&#x5403;&#x996D;&#xFF0C;&#x6CA1;&#x6709;&#x62A2;&#x5230;&#x7684;&#xFF0C;&#x53EA;&#x6709;&#x7B49;&#x5F85;</p>
<p>&#x5B50;&#x7EBF;&#x7A0B;&#xFF1A;&#x5B66;&#x751F;&#x62A2;&#x996D;&#x5361;&#xFF0C;&#x5403;&#x5B8C;&#x5F52;&#x8FD8;&#x996D;&#x5361;</p>
<p><br></p>
<p>&#x65B0;&#x5EFA;&#x4E00;&#x4E2A;&#x5B50;&#x7EBF;&#x7A0B;&#x7C7B;&#xFF1A;&#x6A21;&#x62DF;&#x5B66;&#x751F;&#x62A2;&#x996D;&#x5361;</p>
<pre><code class="lang-java">
<span class="hljs-comment">/**
 * <span class="hljs-doctag">@description</span>
 *    &#x6A21;&#x62DF;&#x4E00;&#x4E2A;&#x5403;&#x996D;&#x7EBF;&#x7A0B;
 * <span class="hljs-doctag">@author</span> TianwYam
 * <span class="hljs-doctag">@date</span> 2021&#x5E74;7&#x6708;1&#x65E5;&#x4E0A;&#x5348;9:19:31
 */</span>
<span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">EatFoodThread</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">Thread</span> </span>{

    <span class="hljs-keyword">private</span> String name ;

    <span class="hljs-keyword">private</span> Semaphore semaphore ;

    <span class="hljs-function"><span class="hljs-keyword">private</span> <span class="hljs-title">EatFoodThread</span><span class="hljs-params">(String name, Semaphore semaphore)</span> </span>{
        <span class="hljs-keyword">this</span>.name = name ;
        <span class="hljs-keyword">this</span>.semaphore = semaphore ;
    }


    <span class="hljs-meta">@Override</span>
    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">run</span><span class="hljs-params">()</span> </span>{

        <span class="hljs-keyword">while</span> (<span class="hljs-keyword">true</span>) {

            <span class="hljs-comment">// &#x5C1D;&#x8BD5; &#x62A2; &#x996D;&#x5361;</span>
            <span class="hljs-keyword">if</span> (semaphore.tryAcquire()) {
                System.out.println(name + <span class="hljs-string">&quot;&#xFF1A;&#x62A2; &#x5230;&#x996D;&#x5361;&#x4E86;&quot;</span>);
                System.out.println(name + <span class="hljs-string">&quot;&#xFF1A;&#x5403;&#x996D; &quot;</span>);

                <span class="hljs-keyword">try</span> {
                    <span class="hljs-comment">// &#x5403;&#x996D;</span>
                    Thread.sleep(<span class="hljs-number">3</span>);
                } <span class="hljs-keyword">catch</span> (InterruptedException e) {
                    e.printStackTrace();
                }

                System.out.println(name + <span class="hljs-string">&quot;&#xFF1A;&#x5403;&#x5B8C;&#x996D;&#x4E86;&#xFF0C;&#x5F52;&#x8FD8;&#x996D;&#x5361; &quot;</span>);
                semaphore.release();
                <span class="hljs-keyword">break</span> ;
            } <span class="hljs-keyword">else</span> {
                System.out.println(name + <span class="hljs-string">&quot;&#xFF1A;&#x6CA1;&#x6709;&#x62A2;&#x5230;&#x996D;&#x5361;&#xFF0C;&#x9700;&#x8981;&#x7B49;&#x5F85; &#x6216;&#x8005; &#x4E0B;&#x6B21;&#x518D;&#x6765; &quot;</span>);
                <span class="hljs-keyword">try</span> {
                    <span class="hljs-comment">// &#x7B49;&#x996D;&#x5361;</span>
                    Thread.sleep(<span class="hljs-number">1</span>);
                } <span class="hljs-keyword">catch</span> (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }

    }
}
</code></pre>
<p><br></p>
<p>&#x4E3B;&#x7EBF;&#x7A0B;&#xFF1A;</p>
<pre><code class="lang-java">
<span class="hljs-comment">/*
 * &#x540C;&#x6B65;&#x5DE5;&#x5177;&#x7C7B;&#xFF1A;Semaphore &#x4FE1;&#x53F7;&#x91CF;
 * &#x5B9A;&#x4E49;&#x603B;&#x7684;&#x4FE1;&#x53F7;&#x91CF;&#xFF0C;&#x62FF;&#x5230;&#x4E86;&#xFF0C;&#x624D;&#x80FD;&#x6267;&#x884C;&#xFF0C;&#x6267;&#x884C;&#x5B8C;&#x540E;&#xFF0C;&#x5F52;&#x8FD8;&#x4FE1;&#x53F7;&#x91CF;&#xFF0C;&#x6CA1;&#x6709;&#x62FF;&#x5230;&#x9700;&#x8981;&#x7B49;&#x5F85;
 * semaphore.tryAcquire() &#x5C1D;&#x8BD5;&#x83B7;&#x53D6; &#x4FE1;&#x53F7;&#x91CF;
 * semaphore.release() &#x91CA;&#x653E;&#x4FE1;&#x53F7;&#x91CF;
 * &#x53EF;&#x7528;&#x4E8E;&#x6D41;&#x91CF;&#x63A7;&#x5236;&#x3001;&#x524A;&#x5CF0;
 */</span>

<span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title">main</span><span class="hljs-params">(String[] args)</span> </span>{

    Semaphore semaphore = <span class="hljs-keyword">new</span> Semaphore(<span class="hljs-number">2</span>);

    <span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> i = <span class="hljs-number">1</span>; i &lt; <span class="hljs-number">4</span>; i++) {
        <span class="hljs-keyword">new</span> EatFoodThread(<span class="hljs-string">&quot;&#x5B66;&#x751F;&quot;</span> + i, semaphore).start();
    }


}
</code></pre>
<p><br><br></p>
<h3 id="32-&#x8FD0;&#x884C;&#x7ED3;&#x679C;"><a name="32-&#x8FD0;&#x884C;&#x7ED3;&#x679C;" class="anchor-navigation-ex-anchor" href="#32-&#x8FD0;&#x884C;&#x7ED3;&#x679C;"><i class="fa fa-link" aria-hidden="true"></i></a>3.2 &#x8FD0;&#x884C;&#x7ED3;&#x679C;</h3>
<p><br></p>
<pre><code class="lang-java">&#x5B66;&#x751F;<span class="hljs-number">1</span>&#xFF1A;&#x62A2; &#x5230;&#x996D;&#x5361;&#x4E86;
&#x5B66;&#x751F;<span class="hljs-number">2</span>&#xFF1A;&#x62A2; &#x5230;&#x996D;&#x5361;&#x4E86;
&#x5B66;&#x751F;<span class="hljs-number">2</span>&#xFF1A;&#x5403;&#x996D; 
&#x5B66;&#x751F;<span class="hljs-number">1</span>&#xFF1A;&#x5403;&#x996D; 
&#x5B66;&#x751F;<span class="hljs-number">3</span>&#xFF1A;&#x6CA1;&#x6709;&#x62A2;&#x5230;&#x996D;&#x5361;&#xFF0C;&#x9700;&#x8981;&#x7B49;&#x5F85; &#x6216;&#x8005; &#x4E0B;&#x6B21;&#x518D;&#x6765; 
&#x5B66;&#x751F;<span class="hljs-number">3</span>&#xFF1A;&#x6CA1;&#x6709;&#x62A2;&#x5230;&#x996D;&#x5361;&#xFF0C;&#x9700;&#x8981;&#x7B49;&#x5F85; &#x6216;&#x8005; &#x4E0B;&#x6B21;&#x518D;&#x6765; 
&#x5B66;&#x751F;<span class="hljs-number">3</span>&#xFF1A;&#x6CA1;&#x6709;&#x62A2;&#x5230;&#x996D;&#x5361;&#xFF0C;&#x9700;&#x8981;&#x7B49;&#x5F85; &#x6216;&#x8005; &#x4E0B;&#x6B21;&#x518D;&#x6765; 
&#x5B66;&#x751F;<span class="hljs-number">3</span>&#xFF1A;&#x6CA1;&#x6709;&#x62A2;&#x5230;&#x996D;&#x5361;&#xFF0C;&#x9700;&#x8981;&#x7B49;&#x5F85; &#x6216;&#x8005; &#x4E0B;&#x6B21;&#x518D;&#x6765; 
&#x5B66;&#x751F;<span class="hljs-number">2</span>&#xFF1A;&#x5403;&#x5B8C;&#x996D;&#x4E86;&#xFF0C;&#x5F52;&#x8FD8;&#x996D;&#x5361; 
&#x5B66;&#x751F;<span class="hljs-number">1</span>&#xFF1A;&#x5403;&#x5B8C;&#x996D;&#x4E86;&#xFF0C;&#x5F52;&#x8FD8;&#x996D;&#x5361; 
&#x5B66;&#x751F;<span class="hljs-number">3</span>&#xFF1A;&#x62A2; &#x5230;&#x996D;&#x5361;&#x4E86;
&#x5B66;&#x751F;<span class="hljs-number">3</span>&#xFF1A;&#x5403;&#x996D; 
&#x5B66;&#x751F;<span class="hljs-number">3</span>&#xFF1A;&#x5403;&#x5B8C;&#x996D;&#x4E86;&#xFF0C;&#x5F52;&#x8FD8;&#x996D;&#x5361;
</code></pre>
<p><br><br></p>
<footer class="page-footer"><span class="copyright">Copyright @copy tianwyam 2021 all right reserved&#xFF0C;powered by Gitbook</span><span class="footer-modification">&#x6700;&#x540E;&#x4FEE;&#x8BA2;&#x65F6;&#x95F4;&#xFF1A;
2021-07-01 14:28:43
</span></footer>
                                
                                </section>
                            
                        </div>
                    </div>
                
            </div>

            
                
                <a href="java_multi_thread_tools.html" class="navigation navigation-prev " aria-label="Previous page: Java并发工具- CountDownLatch&CyclicBarrier ">
                    <i class="fa fa-angle-left"></i>
                </a>
                
                
                <a href="../java_jvm.html" class="navigation navigation-next " aria-label="Next page: java虚拟机-JVM">
                    <i class="fa fa-angle-right"></i>
                </a>
                
            
        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"title":"Java同步工具类- CountDownLatch&CyclicBarrier&Semaphore  ","level":"4.1.8","depth":2,"next":{"title":"java虚拟机-JVM","level":"5.1","depth":1,"path":"java/java_jvm.md","ref":"java/java_jvm.md","articles":[]},"previous":{"title":"Java并发工具- CountDownLatch&CyclicBarrier ","level":"4.1.7","depth":2,"path":"java/multithread/java_multi_thread_tools.md","ref":"java/multithread/java_multi_thread_tools.md","articles":[]},"dir":"ltr"},"config":{"plugins":["-search","-sharing","github","tbfed-pagefooter","splitter","pageview-count","anchor-navigation-ex","chapter-fold","expandable-chapters-small","livereload"],"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"pluginsConfig":{"tbfed-pagefooter":{"copyright":"Copyright @copy tianwyam 2021","modify_label":"最后修订时间：","modify_format":"YYYY-MM-DD HH:mm:ss"},"chapter-fold":{},"github":{"url":"https://github.com/tianwyam"},"livereload":{},"splitter":{},"lunr":{"maxIndexSize":1000000,"ignoreSpecialCharacters":false},"fontsettings":{"theme":"white","family":"sans","size":2},"highlight":{},"anchor-navigation-ex":{"associatedWithSummary":true,"float":{"floatIcon":"fa fa-navicon","level1Icon":"","level2Icon":"","level3Icon":"","showLevelIcon":false},"mode":"float","multipleH1":true,"pageTop":{"level1Icon":"","level2Icon":"","level3Icon":"","showLevelIcon":false},"printLog":false,"showGoTop":false,"showLevel":false},"pageview-count":{},"expandable-chapters-small":{},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false}},"theme":"default","author":"tianwyam","pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"variables":{},"title":"JAVA学习积累","links":{"sidebar":{"博客Blog":"https://blog.csdn.net/mybook201314"}},"gitbook":"*","description":"积累Java学习过程中的知识点，俗话说：好记性不如烂笔头"},"file":{"path":"java/multithread/java_multi_thread_tools2.md","mtime":"2021-07-01T06:28:43.186Z","type":"markdown"},"gitbook":{"version":"3.2.3","time":"2021-11-03T12:24:01.188Z"},"basePath":"../..","book":{"language":""}});
        });
    </script>
</div>

        
    <script src="../../gitbook/gitbook.js"></script>
    <script src="../../gitbook/theme.js"></script>
    
        
        <script src="../../gitbook/gitbook-plugin-github/plugin.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-splitter/splitter.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-pageview-count/plugin.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-chapter-fold/chapter-fold.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-expandable-chapters-small/expandable-chapters-small.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-livereload/plugin.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-lunr/lunr.min.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-lunr/search-lunr.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
        
    

    </body>
</html>

